APM.MainUserPasswordController = Ember.Controller.extend({

    model: {
        password: '',
        newPassword: ''
    },

    /**
     * @todo
     * 验证后面统一写
    */
    validate: function() {
        var me = this;

        return new Ember.RSVP.Promise(function(resolve, reject) {
            var model = me.get('model');
            var $error = $(".password-modification .pwdErrorInfo");
            if (model.password == '') {
                me.set('formTip', '请输入原密码！');
                $error.css("top","100px");
                return;
            }

            if (model.newPassword == '') {
                me.set('formTip', '请输入新密码！');
                $error.css("top","150px");
                return;
            }

            me.set('formTip', '');
            resolve();
        });
    },
    
    actions: {
        save: function() {
            var me = this, model = this.get('model');
            this.validate().then(function() {
                me.store.ajax("PUT", "configs/usermanage/pwd", {
                    oldPass: model.password,
                    newPass: model.newPassword
                }).then(function(response) {
                    if (response.status == "fail") {
                        me.set('formTip', response.message);
                    } else {
                        alertDialog('密码修改成功！');
                    }
                });
            });
        },
        cancel: function() {
            
        }
    }
});